import { HttpException, Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { AlibabaCloudClient } from 'src/utils/alibabaCloudClient';
import Config from '../utils/alibabaCloudClient/config';
import { TokenDto } from './dto/token.dto';
import { ResultDto } from './dto/resultDto';
import request from '../utils/request';
import { v4 as uuidv4 } from 'uuid';
@Injectable()
export class LargeModelService {
  constructor(private httpService: HttpService) {}
  // //NOTE(@date:2023-12-24 15:34:40 谭人杰): 获取TOKEN 用于机器人问答等
  async getToken(params: TokenDto): Promise<any> {
    const client = new AlibabaCloudClient();
    const client_init = client.init();
    const res = await client_init.createToken({
      agentKey: params?.agentKey || Config.agentkey,
    });
    return res?.body?.data?.token;
  }

  async getResult(params: ResultDto, header: any) {
    console.log(params);
    const res = await request({
      url: `/app/completions`,
      method: 'post',
      headers: {
        Authorization: header['largemodeltoken'],
        'Content-Type': 'application/json; charset=utf-8',
      },
      data: {
        RequestId: params?.RequestId ?? uuidv4(),
        Prompt: params.Prompt,
        AppId: params?.AppId ?? Config.AppId,
        Stream: params?.Stream ?? false,
        // SessionId: params?.sessionId,
        // History: true
      },
    });
    return res;
  }
}
